package com.why.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.blog.bean.Blog;
import com.why.blog.bean.Type;
import com.why.blog.service.BlogService;
import com.why.blog.service.typeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;


/**
 * @program: blog
 * @description:
 * @author: @why
 * @create: 2021-01-21 17:13
 **/
@Slf4j
@Controller("adminTypeController")
public class typeController {


    @Autowired
    private typeService type;

    @Autowired
    private BlogService blog;

    /**
     * 得到所有分类以及其对应的博客
     *
     * @return
     */

    @RequestMapping("/type")
    public String typeBlog(Model model, @RequestParam(value = "typeId", defaultValue = "-6") Integer typeId
           ,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNo) {
        List<Type> listType = type.getListBlogByType();
        //如果id为空，则使用默认值
        if(typeId==-6){
            typeId = listType.get(0).getId();
        }

        log.info(String.valueOf(typeId));
        PageHelper.startPage(pageNo,6);
        //根据分类id得到博客
        PageInfo<Blog> listBlogByType = new PageInfo<>(blog.getListBlogByType(typeId));
        model.addAttribute("listType", listType);
        model.addAttribute("typeId", typeId);
        model.addAttribute("blogs", listBlogByType);

        return "type";
    }




}
